package com.yysjyr.logisticssystem.workbench.controller.emp;

import com.yysjyr.logisticssystem.common.ReturnObject;
import com.yysjyr.logisticssystem.common.contants.Contants;
import com.yysjyr.logisticssystem.common.util.DateTimeUtils;
import com.yysjyr.logisticssystem.common.util.IDUtils;
import com.yysjyr.logisticssystem.common.util.OrderPoiUtils;
import com.yysjyr.logisticssystem.settings.entity.DicValue;
import com.yysjyr.logisticssystem.settings.entity.User;
import com.yysjyr.logisticssystem.settings.service.DicValueService;
import com.yysjyr.logisticssystem.workbench.entity.Order;
import com.yysjyr.logisticssystem.workbench.service.OrderService;
import jakarta.servlet.http.HttpSession;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@RestController("order1")
@RequestMapping("/workbench/emp/order")
public class OrderController {

    @Autowired
    private DicValueService dicValueService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("index")
    public ModelAndView toAdd(Model model){
        List<DicValue> goods = dicValueService.queryDicValuesByTypeCode("3");
        model.addAttribute("goods",goods);
        return new ModelAndView("pages/workbench/emp/order/index.html");
    }

    @RequestMapping("saveCreateOrder")
    public ReturnObject saveOrder(Order order,HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);

        order.setId(IDUtils.getOrderId());
        order.setCreateTime(DateTimeUtils.formatDateTime(new Date()));
        order.setCreateBy(user.getId());
        order.setEditFlag("0");
        order.setState(((DicValue)dicValueService.queryFirstDicValueByTypeCode("4")).getId());

        int i = orderService.saveCreateOrder(order);

        if (i != 0){
            return ReturnObject.Ok();
        }
        return ReturnObject.NO();
    }

    @RequestMapping("saveOrderByExcel")
    public ReturnObject saveOrderByExcel(MultipartFile orderFile) throws IOException {
        if (orderFile.isEmpty()) {
            return ReturnObject.NO("上传的Excel文件不能为空");
        }
        String fileName = orderFile.getOriginalFilename();
        if (!fileName.endsWith(".xlsx")) {
            return ReturnObject.NO("只支持.xlsx格式的Excel文件");
        }
        InputStream is = orderFile.getInputStream();
        Workbook workbook = new XSSFWorkbook(is);

        List<Order> orders = OrderPoiUtils.excelToOrderList(workbook);
        orders.remove(0);
        int i = orderService.saveCreateOrder(orders);
        if (i != 0) return ReturnObject.Ok();
        return ReturnObject.NO("数据插入失败");
    }
}
